package com.atguigu.tingshu.album.service.impl;

import com.atguigu.tingshu.album.mapper.*;
import com.atguigu.tingshu.album.service.BaseCategoryService;
import com.atguigu.tingshu.common.anno.GuiGuCache;
import com.atguigu.tingshu.common.constant.RedisConstant;
import com.atguigu.tingshu.model.album.*;
import com.atguigu.tingshu.vo.album.AlbumCategoryVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
@SuppressWarnings({"unchecked", "rawtypes"})
public class BaseCategoryServiceImpl extends ServiceImpl<BaseCategory1Mapper, BaseCategory1> implements BaseCategoryService {

    @Autowired
    private BaseCategoryViewMapper baseCategoryViewMapper;
    @Autowired
    private BaseAttributeMapper baseAttributeMapper;

    @Autowired
    private BaseCategory2Mapper baseCategory2Mapper;

    @Autowired
    private BaseCategory3Mapper baseCategory3Mapper;


    @Override
    public List<AlbumCategoryVo> getBaseCategoryList() {
        return baseCategoryViewMapper.getBaseCategoryList();
    }

    @Override
    public List<BaseAttribute> findAttributeByCategoryId(Integer category1Id) {
        return baseAttributeMapper.findAttributeByCategoryId(category1Id);
    }

    @GuiGuCache(cacheKey = RedisConstant.CATEGORY_INFO_SUFFIX + "#{#params[0]}", enableLock = true, lockName = RedisConstant.CATEGORY_LOCK_SUFFIX + "#{#params[0]}")
    @Override
    public BaseCategoryView findCategoryInfoByC3Id(Long category3Id) {
        return baseCategoryViewMapper.selectById(category3Id);

    }

    @Override
    public AlbumCategoryVo getAllCategoryList(Long category1Id) {
        return baseCategoryViewMapper.getAllCategoryList(category1Id);

    }

    @Override
    public List<BaseCategory3> findTopBaseCategory3(Long category1Id) {
        //根据一级分类id查询二级分类数据
        List<BaseCategory2> baseCategory2List = baseCategory2Mapper.selectList(new LambdaQueryWrapper<BaseCategory2>()
                .eq(BaseCategory2::getCategory1Id, category1Id)
                .select(BaseCategory2::getId)
        );
        List<Long> baseCategory2IdList = baseCategory2List.stream().map(BaseCategory2::getId).collect(Collectors.toList());
        //根据二级分类id查询三级分类数据
        return baseCategory3Mapper.selectList(new LambdaQueryWrapper<BaseCategory3>().in(BaseCategory3::getCategory2Id, baseCategory2IdList)
                .eq(BaseCategory3::getIsTop, 1)
                .last("limit 7"));
    }
}
